<?php

namespace Aiweline\AiReviewForMagentoProduct\Lib\Magento;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class Product
{
    static public function all(int $store_id, int $page = 1, int $pageSize = 10)
    {
        $adminToken = AdminToken::token();
        $apiUrl = Config::api('products');
        $client = new Client();
        try {
            $response = $client->request('get', $apiUrl, [
                'headers' => [
                    'Content-Type' => 'application/json',
                    'accept' => 'application/json',
                    'Authorization' => 'Bearer ' . $adminToken
                ],
                'verify' => false // 去掉ssl验证
                ,
                'query' => [
                    'searchCriteria[pageSize]' => $pageSize,
                    'searchCriteria[currentPage]' => $page,
                    'searchCriteria[filterGroups][0][filters][0][field]' => 'store_id',
                    'searchCriteria[filterGroups][0][filters][0][value]' => $store_id,
                    'searchCriteria[filterGroups][0][filters][0][condition_type]' => 'eq',
                    # 可见产品
                    'searchCriteria[filterGroups][0][filters][1][field]' => 'visibility',
                    'searchCriteria[filterGroups][0][filters][1][value]' => '4',
                    'searchCriteria[filterGroups][0][filters][1][condition_type]' => 'eq',
                ]
            ]);
        } catch (GuzzleException $e) {
        }
        if ($response->getStatusCode() !== 200) {
            // 打印响应体 body 和响应头
            dd($response);
            return;
        }
        $responseData = json_decode($response->getBody()->getContents(), true);
        return $responseData;
    }
}